package tr.helper.controller.server;

import cn.hutool.core.io.FileUtil;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import tr.helper.CommonUtil;
import tr.helper.repository.DataInitialization;

public class ServerMain extends Application {

    public static Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        final DataInitialization initialization = new DataInitialization();
        try {
            initialization.initSchema(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        stage = primaryStage;
        stage.setTitle("服务器部署");
        stage.setScene(new Scene(FXMLLoader.load(ServerMain.class.getResource("ServerList.fxml"))));
        stage.show();

        // 设置主窗口关闭时的行为
        // 用户尝试关闭窗口时触发
        primaryStage.setOnCloseRequest(this::doExit);
    }

    private void doExit(WindowEvent event) {
        // 防止窗口正常关闭
        event.consume();

        // 关闭平台，这将触发Java虚拟机退出
        Platform.exit();

        // 强制退出（如果上述方法没有退出）
        System.exit(0);
    }

    @Override
    public void stop() throws Exception {
        FileUtil.del(CommonUtil.getHomeImagePath());
        CommonUtil.log("exist-->");
    }

    public static void main(String[] args) {
        launch(args);
    }

}
